package com.xinrui.sunnyday.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.xinrui.sunnyday.dao.MemberDao;
import com.xinrui.sunnyday.dao.OrderDao;
import com.xinrui.sunnyday.dao.OrdersettingDao;
import com.xinrui.sunnyday.entity.OrderInfo;
import com.xinrui.sunnyday.exception.SunnydayException;
import com.xinrui.sunnyday.pojo.Member;
import com.xinrui.sunnyday.pojo.Order;
import com.xinrui.sunnyday.pojo.OrderSetting;
import com.xinrui.sunnyday.service.OrderService;
import com.xinrui.sunnyday.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = OrderService.class)
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderDao orderDao;

    @Autowired
    private OrdersettingDao ordersettingDao;

    @Autowired
    private MemberDao memberDao;


    /**
     * 预约提交
     * @param paraMap
     * @return
     * @throws SunnydayException
     */
    @Override
    public Order orderSubmit(Map<String, String> paraMap) throws SunnydayException {
        //获取前端传递过来的时间
        String orderDateStr = paraMap.get("orderDate");
        //从数据库拿到的时间是Date类型，前台传递的是String类型
        //转换时间格式
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date orderDate = null;
        try {
            orderDate = simpleDateFormat.parse(orderDateStr);
        } catch (ParseException e) {
            throw new SunnydayException("日期格式转换失败");
        }
//        获取日期查询是否存在
        OrderSetting orderSetting = ordersettingDao.findByOrderDate(orderDate);
        //判断当前日期是否可预约
        if (null == orderSetting){
            //不可预约
            throw new SunnydayException("当前日期，不可预约");
        }
        //可以预约
        //判断当前日期预约的人数是否约满
        if (orderSetting.getReservations()>=orderSetting.getNumber()){
            //已约满
            throw new SunnydayException("当前日期，预约人数已满");
        }
        //获取前端传递的手机号
        String telephone = paraMap.get("telephone");
        //查询是否为会员
        Member member = memberDao.findByTelephone(telephone);
        if (null == member){
            //非会员
            member = new Member();
            member.setIdCard(paraMap.get("idCard"));
            member.setName(paraMap.get("name"));
            member.setPhoneNumber(telephone);
            member.setRegTime(new Date());
            member.setSex(paraMap.get("sex"));

            //添加入会员表
            memberDao.add(member);
        }
        //是会员
        //进行订单操作
        Order order = new Order();
        order.setMemberId(member.getId());
        order.setOrderDate(orderDate);
        order.setSetmealId(Integer.valueOf(paraMap.get("setmealId")));
        Order orderList = orderDao.findByCondition(order);
        //查询是否重复预约
        if (null != orderList){
            throw new SunnydayException("不能重复预约");
        }
        //没有重复预约
        //补充订单需要添加的信息
        order.setOrderType(Order.ORDERTYPE_WEIXIN);
        order.setOrderStatus(Order.ORDERSTATUS_NO);
        //添加订单
        orderDao.add(order);
        //刷新已预约人数
        ordersettingDao.editReservationsByOrderDate(orderSetting);
        return order;
    }


    /**
     * 预约成功的回显
     * @param id
     * @return
     */
    @Override
    public Map<String, Object> findById4Detail(Integer id) {
        return orderDao.findById4Detail(id);
    }
}
